package com.i7time.qutan.utils;

import com.i7time.entity.User;
import org.json.JSONObject;

import javax.ws.rs.client.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.HashMap;

/**
 * Created by Hsia on 16/6/17.
 */
public class IMUtil {
    private static final String url="https://a1.easemob.com/i7time/i7time";
    private static String token="";
    static{
        getToken();
    }


    public static void main(String[] args) {


       //addUser(user);
       // modifyUserPass("1","12345678");
        //System.out.println(createChatroom("趣探","qutankeji",3000,"1"));

        //System.out.println(getToken());
        delFriend("10056","10057");
    }


    /**
     * 增加IM用户
     * @param username
     * @return
     */
    public static boolean addUser(String username){

        HashMap hash=new HashMap<String,String>();
        hash.put("username",username);
        hash.put("password","qutan2016");

        Client client=ClientBuilder.newClient();
        WebTarget target=client.target(url+"/users/");
        Invocation.Builder builder=target.request().header("Authorization","Bearer "+token);

        Response response=builder.buildPost(Entity.entity(hash, MediaType.APPLICATION_JSON_TYPE)).invoke();
        String result=response.readEntity(String.class);
        JSONObject json;
        json=new JSONObject(result);
        if(json.keySet().contains("error")){
            System.out.println(json.get("error"));
            getToken();
            return false;
        }
        else{
            return true;
        }
    }

    /**
     * 修改用户密码
     * @param userid
     * @param newpass
     * @return
     */
    public static boolean modifyUserPass(String userid,String newpass){

        HashMap hash=new HashMap<String,String>();
        hash.put("newpassword",newpass);


        Client client=ClientBuilder.newClient();
        WebTarget target=client.target(url+"/users/"+userid+"/password");
        Invocation.Builder builder=target.request().header("Authorization","Bearer "+token);

        Response response=builder.buildPost(Entity.entity(hash, MediaType.APPLICATION_JSON_TYPE)).invoke();
        String result=response.readEntity(String.class);
        JSONObject json;
        json=new JSONObject(result);
        System.out.println(json);
        if(json.keySet().contains("error")){
            getToken();
            return false;
        }
        else{
            return true;
        }
    }


    /**
     * 创建聊天室
     * @param sceneName
     * @param descript
     * @param maxusers
     * @param owner
     * @return
     */
    public static String  createChatroom(String sceneName,String descript,int maxusers,String owner) {

        HashMap hash = new HashMap<String, String>();
        hash.put("name", sceneName);
        hash.put("description", descript);
        hash.put("maxusers", maxusers);
        hash.put("owner", owner);

        Client client = ClientBuilder.newClient();
        WebTarget target = client.target(url + "/chatrooms/");
        Invocation.Builder builder = target.request().header("Authorization", "Bearer " + token);

        Response response = builder.buildPost(Entity.entity(hash, MediaType.APPLICATION_JSON_TYPE)).invoke();
        String result = response.readEntity(String.class);
        JSONObject json;
        json = new JSONObject(result);
        // System.out.println(json);
        if (json.keySet().contains("error")) {
            getToken();
            return "0";
        } else {
            return json.getJSONObject("data").getString("id");
        }
    }


    /**
     * 添加好友关系
     * @param owner
     * @param friend
     */
    public static void addFriend(String owner,String friend){
        HashMap hash = new HashMap<String, String>();

        Client client = ClientBuilder.newClient();
        WebTarget target = client.target(url + "/users/"+owner+"/contacts/users/"+friend);
        Invocation.Builder builder = target.request().header("Authorization", "Bearer " + token);

        Response response = builder.buildPost(Entity.entity(hash, MediaType.APPLICATION_JSON_TYPE)).invoke();
        String result = response.readEntity(String.class);
        JSONObject json;
        json = new JSONObject(result);
        // System.out.println(json);
        if (json.keySet().contains("error")) {
            System.out.println(json.get("error"));
            getToken();
            try {
                Thread.sleep(1000l);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            addFriend(owner,friend);

        } else {
            return;
        }
    }



    /**
     * 解除好友关系
     * @param owner
     * @param friend
     */
    public static void delFriend(String owner,String friend){
        HashMap hash = new HashMap<String, String>();

        Client client = ClientBuilder.newClient();
        WebTarget target = client.target(url + "/users/"+owner+"/contacts/users/"+friend);
        Invocation.Builder builder = target.request().header("Authorization", "Bearer " + token);

        Response response = builder.buildDelete().invoke();
        String result = response.readEntity(String.class);
        JSONObject json;
        json = new JSONObject(result);
        // System.out.println(json);
        if (json.keySet().contains("error")) {
            getToken();
            try {
                Thread.sleep(1000l);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            delFriend(owner,friend);

        } else {
            return;
        }
    }

    /**
     * 获取聊天室信息
     * @return
     */
    public static boolean  getChatroom(){


        Client client=ClientBuilder.newClient();
        WebTarget target=client.target(url+"/chatrooms/");
        Invocation.Builder builder=target.request().header("Authorization","Bearer "+token);

        Response response=builder.buildGet().invoke();
        String result=response.readEntity(String.class);
        JSONObject json;
        json=new JSONObject(result);
         System.out.println(json);
        if(json.keySet().contains("error")){
            getToken();
            return false;
        }
        else{
            return true;
        }



    }










    public static synchronized String getToken(){
        HashMap hash=new HashMap<String,String>();
        hash.put("grant_type", "client_credentials");
        hash.put("client_id", "YXA6kgKl0BgxEeaz52VadhVfNQ");
        hash.put("client_secret", "YXA6sHkEPojQa0rpqBimqdJEJXJmbmQ");

        System.out.println("****获取token****");
        Client client= ClientBuilder.newClient();
        WebTarget target=client.target(url+"/token");
        Response response=target.request().buildPost(Entity.entity( hash, MediaType.APPLICATION_JSON_TYPE)).invoke();
//      将HTTP响应打印出来
        String result=response.readEntity(String.class);
        JSONObject json;
        json=new JSONObject(result);
        token=json.getString("access_token");
        System.out.println(token);
        return token;

    }

}
